<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$model = $this->LoadModel('Public');

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();
$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
$nowtime = date('Y-m-d', NOW_TIME);
$now_time = str_replace('-',"",$nowtime);
$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();
$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$dp_code.'&sign='.$sign['sign'];
if($member_info['u_role'] == 6){
  $branch_msg = $model->getOneRow('branch','*',array('`br_id`=?' =>$member_info['u_dp_id']));
  $baoe_data = $model->getList('baoe_month','*',array('`be_branch` =?' => $member_info['u_dpname']),array(1,3),null,array('be_month DESC'));
  $banner_data['baoe_data'] = $baoe_data;
  if(!empty($branch_msg['br_audit_details'])){
    $banner_data['audit_details'] = $branch_msg['br_audit_details'];
  }else{
    if($branch_msg['br_audit_value'] == 0){
      $banner_data['audit_details'] = '无简审';
    }elseif($branch_msg['br_audit_value'] >=10){
      $banner_data['audit_details'] = $branch_msg['br_audit_value'].'万（含）以下简审';
    }else{
      $banner_data['audit_details'] = $branch_msg['br_audit_value'].'万（含）以下双免简审';
    }
  }
  $this->Assign('banner_data', $banner_data);
}
if(empty($this->Param['tp'])){
	echo $this->GetView('order_pendinglist.php');
}else{
	switch ($this->Param['tp']){
		case 'west'://车资待处理
			$where = array(
    		'`o_adtime` >?' => 0 ,
    		'`o_ad_money` >?' => 0 ,
    		'`o_status` NOT IN (?)' => array('退单','作废'),
    		'`o_pact_totime` = ? OR o_pact_totime IS NULL'=> 0,
    		//'`o_pact_gettime` =? OR o_pact_gettime IS NULL OR o_cprice_date IS NULL OR o_cprice_date = 0' =>0,
        '`o_cprice_date` = ? OR o_cprice_date IS NULL' =>0,
    		'`o_adtime` <= ?' =>$now_time
    	);
			$data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_adtime ASC'));
      		$json_data = array('total' => $data['count'], 'rows' => array());
      		if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
            	$row['handle_level'] = NOW_TIME - strtotime($row['o_adtime']) >= 259200 ? 1 : 0;
              if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
              } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
              }
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
			break;
		case 'center'://代偿待处理
			$where = array(
				'`o_tc_complete_date` <= ?' => $now_time,
				'`o_wd_handle` =?' => 0,
        '`o_cofinish_date` = ? OR o_cofinish_date IS NULL' => 0
			);
			$data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_tc_complete_date ASC'));
			if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
            	$row['o_bank_time'] = empty($row['o_bank_time'])? '':date('Y-m-d', strtotime($row['o_bank_time']));
            	$row['o_tc_complete_date'] = empty($row['o_tc_complete_date'])? '':date('Y-m-d', strtotime($row['o_tc_complete_date']));
              if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
              } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
              }
              	$json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
			break;
		case 'east'://问题件待处理
			$where = array(
				'`o_adtime` >?' =>0,
        '`o_bank_time` = ? OR o_bank_time IS NULL' => 0,
				'`o_mp_quit_status` = ?' =>'quit',
				'`o_mp_deal_status` = ?' =>'ok'
			);
			$data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_mp_quit_fortime ASC'));
			if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
              } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
              }
            	$row['handle_level'] = NOW_TIME > (strtotime($row['o_mp_quit_fortime'])+86400)? 1 : 0;
            	$row['o_mp_quit_fortime'] = empty($row['o_mp_quit_fortime'])? '':date('Y-m-d', strtotime($row['o_mp_quit_fortime']));
              	$json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
			break;
		case 'south'://抵押待处理
			$where = array(
       		'`o_bank_money`>?' => 0,
       		'`o_dl_date` = ? OR o_dl_date IS NULL'=> 0,
       		'`o_status` NOT IN(?)' => array('退单','作废','已结清','已代偿'),
       		'`o_pledge_towgdate` > ?' => 0,
       		'`o_bank_time` <= ?' => $now_time
      	);
			$data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_pledge_towgdate ASC'));
      		$json_data = array('total' => $data['count'], 'rows' => array());
        	if (!empty($data['all']))
            	foreach ($data['all'] AS $rowNum => $row) {
                if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
                } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
                }
            		$row['o_pledge_finishdate'] = strtotime($row['o_pledge_towgdate'])+1728000;
            		if(NOW_TIME - strtotime($row['o_pledge_towgdate'])>=1728000){
            			$row['handle_level'] = 2;
            		}elseif(NOW_TIME - strtotime($row['o_pledge_towgdate'])<1728000 && NOW_TIME - strtotime($row['o_pledge_towgdate'])>=1296000){
            			$row['handle_level'] = 1;
            		}else{
            			$row['handle_level'] = 0;
            		}
            		$row['o_pledge_finishdate'] = empty($row['o_pledge_finishdate'])? '':date('Y-m-d', $row['o_pledge_finishdate']);
              		$row['o_bank_time'] = empty($row['o_bank_time'])? '':date('Y-m-d', strtotime($row['o_bank_time']));
              		$row['o_pledge_towgdate'] = empty($row['o_pledge_towgdate'])? '':date('Y-m-d', strtotime($row['o_pledge_towgdate']));
              		$json_data['rows'][] = $row;
            	}
       		echo json_encode($json_data);
			break;
    case 'south2'://合同未收到
      $where = array(
        '`o_adtime` >?' => 0 ,
        '`o_ad_money` >?' => 0 ,
        '`o_status` NOT IN (?)' => array('退单','作废'),
        '`o_pact_totime` = ? OR o_pact_totime IS NULL'=> 0,
        //'`o_pact_gettime` =? OR o_pact_gettime IS NULL OR o_cprice_date IS NULL OR o_cprice_date = 0' =>0,
        '`o_pact_gettime` =? OR o_pact_gettime IS NULL' => 0,
        '`o_adtime` <= ?' =>$now_time
      );
      $data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_adtime ASC'));
          $json_data = array('total' => $data['count'], 'rows' => array());
          if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              $row['handle_level'] = NOW_TIME - strtotime($row['o_adtime']) >= 259200 ? 1 : 0;
              if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
              } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
              }
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
      break;
    case 'south3'://GPS待上线
    $time = date('Y-m-d', NOW_TIME-432000);
    $time = str_replace('-',"",$time);
      $where = array(
        '`o_adtime` >=?' => 20180525,
        '`o_adtime` <=?' => $time,
        '`o_ad_money` >?' => 0,
        '`o_status` NOT IN (?)' => array('退单','作废'),
        '`o_gps_status` != ?' => '已上线',
        '`o_needgps` =?' =>1
      );
      $data = $OrderHelper->orderList($where, 1, 10000, $this->Param, $member_info,'postloan_info',array('o_adtime ASC'));
          $json_data = array('total' => $data['count'], 'rows' => array());
          if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              //$row['handle_level'] = 0;
              //if($row['o_car_pose'] == '新车' && $row['o_adtime'] >= 20180525){
                $row['handle_level'] = NOW_TIME - strtotime($row['o_adtime']) >= 432000 ? 1 : 0;
              //}
              $row['o_gps_install'] = $row['o_gps_install'] == '0000-00-00'? '':$row['o_gps_install'];
              $row['o_adtime'] = empty($row['o_adtime'])? '':date('Y-m-d', strtotime($row['o_adtime']));
              if ($row['o_oldsys'] == 1) {
                  $sign = $oldapi->sign(array('code' => $dp_code));
                  $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
              } else {
                  $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
              }
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
      break;
	default;
	}
}
